package main

import (
	"fmt"
	"time"
)

func main() {
	// 1. 输出当前时间
	// time 对象
	time1 := time.Now()

	fmt.Println(time1) //2023-11-05 21:39:04.4268231 +0800 CST m=+0.003089901

	year := time1.Year()
	month := time1.Month()
	day := time1.Day()
	hour := time1.Hour()
	minute := time1.Minute()
	second := time1.Second()

	fmt.Printf("%d-%02d-%02d %02d:%02d:%02d\n", year, month, day, hour, minute, second) //2023-11-05 21:42:27

	// 2. 输出当前时间 ==> 格式化模板  12小时制
	time2 := time.Now()
	resDate := time2.Format("2006-01-02 03:04:05")
	fmt.Printf("%v \n", resDate)

	// 24小时制
	resDate = time2.Format("2006-01-02 15:04:05")
	fmt.Printf("%v \n", resDate)

	// 获取时间戳
	time3 := time.Now()
	unixtime := time3.Unix()       // 时间戳 ==> 秒
	nanotime := time3.UnixNano()   // 纳秒
	microtime := time3.UnixMicro() // 毫秒
	millitime := time3.UnixMilli() // 微秒
	fmt.Println(unixtime, nanotime, millitime, microtime)

	// 时间戳转时间
	fmt.Println(time.Unix(unixtime, 0)) //2023-11-05 21:54:29 +0800 CST
	// 日期转时间戳
	time4, _ := time.ParseInLocation("2006-01-02 15:04:05", "2023-11-05 21:56:23", time.Local)
	fmt.Println(time4.Unix()) //1699192583

	time5 := time.Now()
	fmt.Println(time5) //2023-11-05 21:59:48.8555177 +0800 CST m=+0.017002501
	// 增加时间
	// time5 = time5.Add(time.Hour * 24)
	fmt.Println(time5) //2023-11-06 21:59:48.8555177 +0800 CST m=+86400.017002501

	// 定时器
	// time包内定时器
	time6 := time.NewTicker(time.Second * 2)
	n := 5
	for key := range time6.C {
		n--
		fmt.Println(key) // 每两秒一次、
		if n == 0 {
			/*
				2023-11-05 22:04:07.836262 +0800 CST m=+2.020884301
				2023-11-05 22:04:09.8443733 +0800 CST m=+4.028995601
				2023-11-05 22:04:11.8438173 +0800 CST m=+6.028439601
				2023-11-05 22:04:13.8364324 +0800 CST m=+8.021054701
				2023-11-05 22:04:15.8331113 +0800 CST m=+10.017733601
			*/
			time6.Stop()
			break
		}
	}

}
